<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="指派司机"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="360px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="司机" prop="driverId">
                <el-select
                    v-model="formData.driverId"
                    filterable
                    placeholder="请选择"
                >
                    <el-option
                        v-for="item in driverOptions"
                        :key="item.id"
                        :label="item.driverName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>

            <el-form-item>

            </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-popconfirm :title="'确定排班吗？'" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">确定</el-button>
            </el-popconfirm>
        </span>
    </el-dialog>
</template>

<script>
import {ArrangeApi} from '@/api/intercity/ArrangeApi'
import {DriverApi} from '@/api/user/DriverApi'
import {Common} from '@/utils/common'

export default {
    name: 'AppointDriver',
    components: {},
    data() {
        return {
            formData: {
                arrangeCode: '',
                driverId: ''
            },
            rules: {
                driverId: [
                    {required: true, message: '请选择司机', trigger: 'change'}
                ]
            },
            dialogVisible: false,
            driverOptions: []
        }
    },
    methods: {
        // 显示组件
        show(item) {
            this.dialogVisible = true
            this.formData.arrangeCode = item.arrangeCode
            this.formData.driverId = item.driverId ?item.driverId:''
            this.getDriverOptions()
        },

        // 获取线路列表
        getDriverOptions() {
            DriverApi.getList({status:true}).then((data) => {
                this.driverOptions = data
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    const data = Object.assign({}, that.formData)

                    const loading = that.$loading({
                              lock: true,
                              text: '请稍候...',
                              spinner: 'el-icon-loading',
                              background: 'rgba(0, 0, 0, 0.7)'
                            });
                    ArrangeApi.appointDriver(data).then((response) => {
                        loading.close()
                        that.dialogVisible = false
                        that.$emit('refresh')
                    }).catch(()=>{
                        loading.close()
                    })
                }
            })
        }
    }
}
</script>
